<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2020-11-15 11:14:38
 */

namespace app\common\controller;
use app\admin\logic\AdminUser as AdminUserLogic;
use think\Cookie;
use think\facade\Config as thinkConfig;

class Backend extends Base {

	protected $admindb;
	protected $adminuid; //用户uid
	protected $adminname; //用户名
	protected $businessuid; //该管理员绑定前台用户

	public function initialize() {
		parent::initialize();

		$adminuid = AdminUserLogic::isLogin();
		if (!$adminuid) {
			// 还没登录 跳转到登录页面
			$this->redirect('admin/login/index');
			exit;
		}

		if (!in_array($this->urlRule, ['admin/login/index', 'admin/index/logout'])) {
			// 检测系统权限
			// if(!is_administrator()){
			// 	if (config('admin_allow_ip')) {
			// 		// 检查IP地址访问
			// 		if (!in_array($this->ip, explode(',', config('admin_allow_ip')))) {
			// 			$this->error('403:禁止访问');
			// 		}
			// 	}
			// 	$this->checkAuth();
			// }
			$adminUserDao = model('admin/AdminUser');
			$admindb = $adminUserDao->getInfo($adminuid);

			if ($admindb['groupid'] == 1) { //超级管理员
				$admindb['is_super'] = 1;
			}

			$this->adminuid = $adminuid;
			$this->adminname = $admindb['username'];
			$this->businessuid = $admindb['business_uid']; //该管理员绑定前台用户
			$this->admindb = $admindb;

			$this->assign('adminuid', $adminuid);
			$this->assign('adminname', $admindb['username']);
			$this->assign('admindb', $admindb);

			$P_S_T	 = pwMicrotime();
			//获取菜单
			list($isSelfmenu, $sideBarMenuCate, $sidebarMenuGroup, $subsidiaryArr) = logic('admin/AdminAuth')->getAdminSidebarMenu($admindb['groupid']);
			//echo '是否独立菜单:';echo $isSelfmenu;echo '菜单分类:';print_r($sideBarMenuCate);
			//echo '菜单树:';print_r($sidebarMenuGroup);
			$this->assign("isSelfmenu", $isSelfmenu);
			$this->assign("sideBarMenuCate", $sideBarMenuCate);
			$this->assign("sidebarMenuGroup", $sidebarMenuGroup);

			//权限控制
			$P_S_T2	 = pwMicrotime();
			list($count, $parentRule) = logic('admin/AdminAuth')->checkAuth($admindb['groupid'], $isSelfmenu, $subsidiaryArr);
			//echo '父级菜单:';print_r($parentRule);
			if (!$count) {
				$this->error('您没有访问权限！');
			}
			$this->assign("parentRule", $parentRule);

			//微信公众账号ID(微信公众号有用)
			$wid = input('param.wid', '', 'intval');
			if (!$wid) {
				$wid = input('post.wid', '', 'intval');
			}
			$this->wid = $wid;
			$this->assign("wid", $wid);

			$P_S_T3	 = pwMicrotime();
			if ($wid) {
				//获取微信公众号信息
				$weixinAccountDao = model('weixin_manage/WeixinAccount');
				$wxdb = $weixinAccountDao->getInfo($wid);
				$this->assign("wxdb", $wxdb);
				$this->wxdb = $wxdb;
			}
		}

		$this->assign('dashboardPageUrl', url(config('dashboard_page')));
		$this->assign('_tpl_public_layout_danceadmin', '../application/admin/view/public/layout_danceadmin.html'); //页面公共继承模版

		$time2 = $P_S_T2-$P_S_T;
		$time3 = $P_S_T3-$P_S_T;
		$wind_spend .= '<br>time2:'.$time2.'<br>time3:'.$time3;
		$this->assign('wind_spend', $wind_spend);

		//校验是否同时后台在线多个用户
		// if (!AdminLogic::checkAllowLoginByTime()) {
		// 	$this->error('您的帐号正在别的地方登录!',url('admin/login/logout'));
		// }

		// if(!IS_AJAX){
		// 	$this->assign('current_user',$this->currentUser);

		// 	//是否菜单被收藏
		// 	$collect_menus = config('admin_collect_menus');
		// 	$this->assign('is_menu_collected',0);
		// 	if (isset($collect_menus[$this->request->url()])) {
		// 		$this->assign('is_menu_collected',1);
		// 	}
		// 	if (PUBLIC_RELATIVE_PATH=='') {
		// 		$template_path_str = '../';
		// 	} else{
		// 		$template_path_str = './';
		// 	}

		// 	$_admin_public_base = '';
		// 	if ($this->request->param('load_type')=='iframe') {
		// 		$_admin_public_base = $template_path_str.'application/admin/view/public/layerbase.html';
		// 	} else{
		// 		$_admin_public_base = $template_path_str.'application/admin/view/public/base.html';
		// 	}
		// 	$_admin_public_base = APP_PATH.'admin/view/public/base.html';

		// 	//顶部模版
		// 	$this->assign('_admin_document_header_',$template_path_str.'application/admin/view/public/document_header.html');
		// 	$this->assign('_admin_public_left_',$template_path_str.'application/admin/view/public/left.html');
		// 	$this->assign('_admin_public_base_', $_admin_public_base);
		// 	$this->assign('_admin_public_layerbase_', $template_path_str.'application/admin/view/public/layerbase.html');
		// }

	}

	/**
	 * 排序字段为listorders数组 POST 排序字段为：vieworders
	 */
	protected function listordersOrigin($model) {
		if (!is_object($model)) {
			return false;
		}
		$pk = $model->getPk(); //获取主键名称
		$vieworders = input('post.vieworders/a', array(), 'pwEscape');
		foreach ($vieworders as $key => $value) {
			$data = array();
			$data['vieworder'] = $value;
			$model->where($pk, $key)->update($data);
		}
		return true;
	}

	function saveAndGetConfigIdentify($model, $sideName, $sysName='', $sideFlag='') {
		if (1) {
			$sys_identify = 'system';
			$configInfo = cache($sys_identify);
			if (empty($configInfo)) {
				$config_space = 'global';
				$configDao = model('common/Config');
				$configInfo = $configDao->getConfigsBySpace($config_space);
				cache($sys_identify, $configInfo); //快速缓存
			}
			$this->configInfo = $configInfo;
			$this->assign('configInfo', $configInfo);
		}
		//print_r($configInfo);
		//添加至全局配置
		thinkConfig::set($configInfo);
		
		if ($_SERVER['HTTP_HOST'] == config('dev_host')) {
			$this->setSeoOrigin($sideName);
		} else {
			$this->setSeoOrigin($sideName, $configInfo['site_title']);
		}

		$this->assign('sideName', $sideName);
		$this->assign('sideFlag', $sideFlag);
		$this->assign('sysName', $sysName);
	}
	
	protected function getMapForUser($map, $parameter) {
		$audit = input('param.audit', '', 'intval');
		if ($audit) {
			$map[] = ['audit', '=', $audit];
			$parameter['audit'] = $audit;
		}
		$this->assign('audit', $audit);
		
		$id = input('param.id', '', 'intval');
		if ($id) {
			$map[] = ['id', '=', $id];
			$parameter['id'] = $id;
		}
		$this->assign('id', $id);
		
		$uid = input('param.uid', '', 'intval');
		if ($uid) {
			$map[] = ['uid', '=', $uid];
			$parameter['uid'] = $uid;
		}
		$this->assign('uid', $uid);

		$pwuid = input('param.pwuid', '', 'intval');
		if ($pwuid) {
			$map[] = ['pwuid', '=', $pwuid];
			$parameter['pwuid'] = $pwuid;
		}
		$this->assign('pwuid', $pwuid);
		
		$openid = input('param.openid', '', 'pwEscape');
		if ($openid) {
			$map[] = ['openid', '=', $openid];
			$parameter['openid'] = $openid;
		}
		$this->assign('openid', $openid);

		$unionid = input('param.unionid', '', 'pwEscape');
		if ($unionid) {
			$map[] = ['unionid', '=', $unionid];
			$parameter['unionid'] = $unionid;
		}
		$this->assign('unionid', $unionid);
		
		$username = input('param.username', '', 'pwEscape');
		if ($username) {
			$map[] = ['username', 'like', '%'.$username.'%'];
			$parameter['username'] = $username;
		}
		$this->assign('username', $username);
		
		$gender = input('param.gender', '', 'intval');
		if ($gender) {
			$map[] = ['gender', '=', $gender];
			$parameter['gender'] = $gender;
		}
		$this->assign('gender', $gender);

		$city = input('param.city', '', 'pwEscape');
		if ($city) {
			$map[] = ['city', '=', $city];
			$parameter['city'] = $city;
		}
		$this->assign('city', $city);
		
		$province = input('param.province', '', 'pwEscape');
		if ($province) {
			$map[] = ['province', '=', $province];
			$parameter['province'] = $province;
		}
		$this->assign('province', $province);
		
		$country = input('param.country', '', 'pwEscape');
		if ($country) {
			$map[] = ['country', '=', $country];
			$parameter['country'] = $country;
		}
		$this->assign('country', $country);
		
		$realname = input('param.realname', '', 'pwEscape');
		if ($realname) {
			$map[] = ['realname', 'like', '%'.$realname.'%'];
			$parameter['realname'] = $realname;
		}
		$this->assign('realname', $realname);
		
		$mobile = input('param.mobile', '', 'pwEscape');
		if ($mobile) {
			$map[] = ['mobile', 'like', '%'.$mobile.'%'];
			$parameter['mobile'] = $mobile;
		}
		$this->assign('mobile', $mobile);
		
		$idcard = input('param.idcard', '', 'pwEscape');
		if ($idcard) {
			$map[] = ['idcard', 'like', '%'.$idcard.'%'];
			$parameter['idcard'] = $idcard;
		}
		$this->assign('idcard', $idcard);

		$address = input('param.address', '', 'pwEscape');
		if ($address) {
			$map[] = ['address', 'like', '%'.$address.'%'];
			$parameter['address'] = $address;
		}
		$this->assign('address', $address);
		
		$expand1 = input('param.expand1', '', 'pwEscape');
		if ($expand1) {
			$map[] = ['expand1', 'like', '%'.$expand1.'%'];
			$parameter['expand1'] = $expand1;
		}
		$this->assign('expand1', $expand1);

		$expand2 = input('param.expand2', '', 'pwEscape');
		if ($expand2) {
			$map[] = ['expand2', 'like', '%'.$expand2.'%'];
			$parameter['expand2'] = $expand2;
		}
		$this->assign('expand2', $expand2);

		$expand3 = input('param.expand3', '', 'pwEscape');
		if ($expand3) {
			$map[] = ['expand3', 'like', '%'.$expand3.'%'];
			$parameter['expand3'] = $expand3;
		}
		$this->assign('expand3', $expand3);

		$expand4 = input('param.expand4', '', 'pwEscape');
		if ($expand4) {
			$map[] = ['expand4', 'like', '%'.$expand4.'%'];
			$parameter['expand4'] = $expand4;
		}
		$this->assign('expand4', $expand4);

		//时间筛选 - 双条件搜索
		$start_time = input('param.start_time', '', 'intval');
		if ($start_time) {
			$start_time_s = time2str($start_time);
		} else {
			$start_time_s = input('param.start_time_s', '', 'pwEscape');
			$start_time = str2time($start_time_s);
		}
		$end_time = input('param.end_time', '', 'intval');
		if ($end_time) {
			$end_time_s = time2str($end_time);
		} else {
			$end_time_s = input('param.end_time_s', '', 'pwEscape');
			$end_time = str2time($end_time_s);
		}
		if ($start_time && $end_time) {
			$map[] = ['create_time', '>', $start_time];
			$map[] = ['create_time', '<', $end_time];
			$parameter['start_time'] = $start_time;
			$parameter['end_time'] = $end_time;
		} else {
			if ($start_time) {
				$map[] = ['create_time', '>', $start_time];
				$parameter['start_time'] = $start_time;
			}
			if ($end_time) {
				$map[] = ['create_time', '<', $end_time];
				$parameter['end_time'] = $end_time;
			}
		}
		$this->assign('start_time_s', $start_time_s);
		$this->assign('end_time_s', $end_time_s);
		
		return array($map, $parameter);
	}

}